package com.icode.core.dto.house;

import com.icode.core.model.house.*;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.Pattern;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: xianfei
 * Date: 15-1-13
 * Time: 上午11:41
 * To change this template use File | Settings | File Templates.
 */
public class HouseBaseDTO {
    private String guid;
    private String id;
    @Length(min = 3, message = "名字内容过短!")
    private String title;
    @NotEmpty(message = "请输入度假屋介绍")
    private String introduction;
    @NotEmpty(message = "请输入度假屋详情")
    private String information;
    @Pattern(regexp = "^(\\d{1,3},?){1,4}$", message = "价格不正确!")
    @NotEmpty(message = "请输入价格")
    private String price;
    private int houseTypeNum;
    @NotEmpty(message = "请选择产品类型")
    private String productType;
    private List<MultipartFile> images;
    private List<String> imageGuids;
    private String houseBaseArea;
    private String houseBaseTheme;
    private boolean fineTheme;

    private List<String> buildingTypeGuids;
    private List<String> buildingTypeImageGuids;
    private List<BuildingTypeDTO> buildingTypeDTOs;

    public HouseBaseDTO() {
    }


    public HouseBaseDTO(String guid, String id, String title, String introduction, String information, String price, int houseTypeNum, String productType,
                        List<String> imageGuids, String houseBaseArea, String houseBaseTheme, boolean fineTheme, List<String> buildingTypeGuids,List<BuildingTypeDTO> buildingTypeDTOs) {
        this.guid = guid;
        this.id = id;
        this.title = title;
        this.introduction = introduction;
        this.information = information;
        this.price = price;
        this.houseTypeNum = houseTypeNum;
        this.productType = productType;
        this.imageGuids = imageGuids;
        this.houseBaseArea = houseBaseArea;
        this.houseBaseTheme = houseBaseTheme;
        this.fineTheme = fineTheme;
        this.buildingTypeGuids = buildingTypeGuids;
        this.buildingTypeDTOs = buildingTypeDTOs;
    }

    public HouseBase toHouseBase(List<MultipartFile> images) {
        HouseBase houseBase = new HouseBase(this.title, this.introduction, this.information, this.price, ProductType.valueOf(this.productType), HouseBaseArea.valueOf(this.houseBaseArea), this.houseBaseTheme, this.fineTheme);
        houseBase.initImages(toHouseBaseImage(houseBase, images));
        return houseBase;
    }

    public HouseBase toHouseBase(HouseBaseDTO houseBaseDTO) {
        HouseBase houseBase = new HouseBase(houseBaseDTO.title, houseBaseDTO.introduction, houseBaseDTO.information, houseBaseDTO.price, ProductType.valueOf(this.productType), HouseBaseArea.valueOf(this.houseBaseArea), this.houseBaseTheme, this.fineTheme);
        houseBase.initImages(toHouseBaseImage(houseBase, houseBaseDTO.images));
        return houseBase;
    }

    public HouseBase toHouseBaseNoImage(HouseBaseDTO houseBaseDTO) {
        HouseBase houseBase = new HouseBase(houseBaseDTO.title, houseBaseDTO.introduction, houseBaseDTO.information, houseBaseDTO.price, ProductType.valueOf(this.productType), HouseBaseArea.valueOf(this.houseBaseArea), this.houseBaseTheme, this.fineTheme);
        return houseBase;
    }


    public List<HouseBaseImage> toHouseBaseImage(HouseBase houseBase, List<MultipartFile> images) {
        List<HouseBaseImage> list = new ArrayList<>();
        try {
            for (MultipartFile m : images) {
                UploadImage uploadImage = new UploadImage(m.getName(), m.getContentType(), m.getBytes());
                HouseBaseImage houseBaseImage = new HouseBaseImage(houseBase, uploadImage);
                list.add(houseBaseImage);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }

    public static List<HouseBaseDTO> toDTOs(List<HouseBase> houseBases) {
        List<HouseBaseDTO> houseBaseDTOs = new ArrayList<>();
        for (HouseBase houseBase : houseBases) {
            HouseBaseDTO houseBaseDTO = toDTO(houseBase);
            houseBaseDTOs.add(houseBaseDTO);
        }
        return houseBaseDTOs;
    }

    public static HouseBaseDTO toDTO(HouseBase houseBases) {
        List<BuildingType> buildingTypes = houseBases.buildingTypes();
        List<String> buildingTypeImageGuids =new ArrayList<>();
        int houseTypeNum = buildingTypes.isEmpty() ? 0 : buildingTypes.size();
        List<String> imageGuids = initImageGuids(houseBases.houseBaseImages());
        String houseBaseThems = houseBases.themes();
        List<String> buildingTypeGuids = new ArrayList<>();
        for (BuildingType buildingType : buildingTypes) {
            String buildingTypeGuid = buildingType.guid();
            buildingTypeGuids.add(buildingTypeGuid);
            if(buildingType.buildingTypeImages().size()>0){
                String buildingTypeImageGuid= buildingType.buildingTypeImages().get(0).image().guid();
               buildingTypeImageGuids.add(buildingTypeImageGuid);
            }
            else{
                buildingTypeImageGuids.add("");
            }
        }
        List<BuildingTypeDTO> buildingTypeDTOs = BuildingTypeDTO.toDTOs(buildingTypes);
        HouseBaseDTO houseBaseDTO = new HouseBaseDTO(houseBases.guid(), houseBases.id(), houseBases.title(), houseBases.introduction(), houseBases.information(),
                houseBases.price(), houseTypeNum, houseBases.productType().name(), imageGuids, houseBases.houseBaseArea().getValue(), houseBaseThems, houseBases.fineTheme(), buildingTypeGuids,buildingTypeDTOs);
        return houseBaseDTO;
    }

    public static List<String> initImageGuids(List<HouseBaseImage> houseBaseImages) {
        List<String> imagesGuids = new ArrayList<>();
        for (HouseBaseImage h : houseBaseImages) {
            String guid = h.uploadImage().guid();
            imagesGuids.add(guid);
        }
        return imagesGuids;
    }

    public String getGuid() {
        return guid;
    }

    public void setProductType(String productType) {
        this.productType = productType;
    }

    public String getProductType() {
        return productType;
    }

    public void setGuid(String guid) {
        this.guid = guid;
    }

    public String getTitle() {
        return title;
    }

    public String getIntroduction() {
        return introduction;
    }

    public String getHouseBaseArea() {
        return houseBaseArea;
    }

    public void setHouseBaseArea(String houseBaseArea) {
        this.houseBaseArea = houseBaseArea;
    }

    public String getHouseBaseTheme() {
        return houseBaseTheme;
    }

    public void setHouseBaseTheme(String houseBaseTheme) {
        this.houseBaseTheme = houseBaseTheme;
    }

    public String getInformation() {
        return information;
    }

    public String getPrice() {
        return price;
    }

    public List<MultipartFile> getImages() {
        return images;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void setIntroduction(String introduction) {
        this.introduction = introduction;
    }

    public void setInformation(String information) {
        this.information = information;
    }

    public void setPrice(String price) {
        this.price = price;
    }

    public void setImageGuids(List<String> imageGuids) {
        this.imageGuids = imageGuids;
    }

    public List<String> getImageGuids() {

        return imageGuids;
    }

    public void setImages(List<MultipartFile> images) {
        this.images = images;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public int getHouseTypeNum() {
        return houseTypeNum;
    }

    public void setHouseTypeNum(int houseTypeNum) {
        this.houseTypeNum = houseTypeNum;
    }

    public boolean isFineTheme() {
        return fineTheme;
    }

    public void setFineTheme(boolean fineTheme) {
        this.fineTheme = fineTheme;
    }

    public List<String> getBuildingTypeGuids() {
        return buildingTypeGuids;
    }

    public void setBuildingTypeGuids(List<String> buildingTypeGuids) {
        this.buildingTypeGuids = buildingTypeGuids;
    }

    public List<String> getBuildingTypeImageGuids() {
        return buildingTypeImageGuids;
    }

    public void setBuildingTypeImageGuids(List<String> buildingTypeImageGuids) {
        this.buildingTypeImageGuids = buildingTypeImageGuids;
    }

    public List<BuildingTypeDTO> getBuildingTypeDTOs() {
        return buildingTypeDTOs;
    }

    public void setBuildingTypeDTOs(List<BuildingTypeDTO> buildingTypeDTOs) {
        this.buildingTypeDTOs = buildingTypeDTOs;
    }
}
